package com.chhuang.ewcrm.ctrl;

import com.chhuang.ewcrm.App;
import com.chhuang.ewcrm.comm.Serial;
import com.chhuang.ewcrm.cons.Constants;
import com.chhuang.ewcrm.dao.WWeighMeterDao;
import com.chhuang.ewcrm.entity.WWeighMeter;
import com.chhuang.ewcrm.thread.AddLogThread;
import com.chhuang.ewcrm.utils.AutoCompleteComboBoxListener;
import com.chhuang.ewcrm.utils.LengthTextFilter;
import com.chhuang.ewcrm.utils.MyAlert;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXCheckBox;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.util.StringConverter;

import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
import java.util.function.Consumer;

/**
 * 过磅仪表配置界面控制类
 */
public class WeighMeterController implements InitController{

    @FXML
    private ComboBox<String> brandComboBox;
    @FXML
    private ComboBox<String> commComboBox;
    @FXML
    private ComboBox<Integer> baudrateComboBox;
    @FXML
    private ComboBox<Integer> databitsComboBox;
    @FXML
    private ComboBox<Integer> stopbitsComboBox;
    @FXML
    private ComboBox<Integer> parityComboBox;
    @FXML
    private JFXCheckBox enableJFXCheckBox;
    @FXML
    private JFXButton saveJFXButton;

    private final WWeighMeterDao wWeighMeterDao = App.getSession().getMapper(WWeighMeterDao.class);

    private final LengthTextFilter lengthTextFilter = new LengthTextFilter();

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        WWeighMeter wWeighMeter = wWeighMeterDao.queryById("001");

        StringConverter<Integer> stringConverter = new StringConverter<>() {
            @Override
            public String toString(Integer integer) {
                return integer==null?null:integer.toString();
            }

            @Override
            public Integer fromString(String s) {
                return s==null?null:Integer.parseInt(s);
            }
        };

        brandComboBox.getItems().addAll("柯力");
        brandComboBox.setValue(wWeighMeter.getWmeterBrand());
        //获取计算机串口名称
        List<String> commList = Serial.GetCommNameList();
        commComboBox.getItems().addAll(commList);
        commComboBox.getEditor().setTextFormatter(new TextFormatter<>(lengthTextFilter));
        commComboBox.setValue(wWeighMeter.getWmeterComm());

        baudrateComboBox.getItems().addAll(9600,14400,19200,38400,56000,57600,115200,230400,460800,921600);
        baudrateComboBox.getEditor().setTextFormatter(new TextFormatter<>(lengthTextFilter));
        baudrateComboBox.setValue(wWeighMeter.getWmeterBaudrate());
        new AutoCompleteComboBoxListener<>(baudrateComboBox);
        baudrateComboBox.setConverter(stringConverter);

        databitsComboBox.getItems().addAll(5,6,7,8);
        databitsComboBox.getEditor().setTextFormatter(new TextFormatter<>(lengthTextFilter));
        databitsComboBox.setValue(wWeighMeter.getWmeterDatabits());
        new AutoCompleteComboBoxListener<>(databitsComboBox);
        databitsComboBox.setConverter(stringConverter);

        stopbitsComboBox.getItems().addAll(1,2);
        stopbitsComboBox.getEditor().setTextFormatter(new TextFormatter<>(lengthTextFilter));
        stopbitsComboBox.setValue(wWeighMeter.getWmeterStopbits());
        new AutoCompleteComboBoxListener<>(stopbitsComboBox);
        stopbitsComboBox.setConverter(stringConverter);

        parityComboBox.getItems().addAll(0,1,2,3,4);
        parityComboBox.getEditor().setTextFormatter(new TextFormatter<>(lengthTextFilter));
        parityComboBox.setValue(wWeighMeter.getWmeterParity());
        new AutoCompleteComboBoxListener<>(parityComboBox);
        parityComboBox.setConverter(stringConverter);

        enableJFXCheckBox.setSelected(wWeighMeter.getWmeterEnable());

        //保存
        saveJFXButton.setOnAction(event -> {
            String brand = brandComboBox.getValue();
            String comm = commComboBox.getValue();
            Integer baudrate = baudrateComboBox.getValue();
            Integer databits = databitsComboBox.getValue();
            Integer stopbits = stopbitsComboBox.getValue();
            Integer parity = parityComboBox.getValue();
            Boolean enable = enableJFXCheckBox.isSelected();

            wWeighMeter.setWmeterBrand(brand);
            wWeighMeter.setWmeterComm(comm);
            wWeighMeter.setWmeterBaudrate(baudrate);
            wWeighMeter.setWmeterDatabits(databits);
            wWeighMeter.setWmeterStopbits(stopbits);
            wWeighMeter.setWmeterParity(parity);
            wWeighMeter.setWmeterEnable(enable);

            wWeighMeter.setModifyTime(new Date());
            wWeighMeterDao.update(wWeighMeter);

            new Thread(new AddLogThread(Constants.loginUser.getUserId(),
                    Constants.loginUser.getUsername(),
                    Constants.loginUser.getFullname(),
                    "修改过磅仪表信息成功")).start();

            MyAlert.showAlert("成功","修改成功",null);
        });
    }

    @Override
    public void close() {

    }
}
